package handler

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/liangchao/blogo/src/admin/forms"
	"github.com/liangchao/blogo/src/common/Response"
	"github.com/liangchao/blogo/src/common/dao"
	"github.com/liangchao/blogo/src/common/global"
	"github.com/liangchao/blogo/src/common/models"
	"log"
	"strconv"
	"time"
)

// QueryBlogListByKeywords: 根据关键字查询博客列表
func QueryBlogListByKeywords(c *gin.Context) {
	input := c.Query("input")
	method, _ := strconv.Atoi(c.DefaultQuery("method", "1"))
	page, _ := strconv.Atoi(c.Param("page"))
	limit, _ := strconv.Atoi(c.Param("limit"))

	if method < 0 {
		Response.Err(c, 400, 400, "method must be a positive number", "")
		return
	}

	offset := (page - 1) * limit
	blogs, total, err := new(dao.BlogDao).QueryBlogListByKeyworks(input, method, offset, limit)
	if err != nil {
		Response.Err(c, 400, 400, "cannot find", "")
		return
	}
	Response.Success(c, 200, "success", map[string]interface{}{
		"blogs": blogs,
		"total": total,
	})

}

// GetBlogById 通过 id 查询博客
func GetBlogById(c *gin.Context) {
	id, err := strconv.Atoi(c.Param("id"))
	if err != nil {
		log.Println(err)
		return
	}
	blog, err := new(dao.BlogDao).GetBlogById(id)
	if err != nil {
		return
	}
	bc, err := new(dao.BlogDao).GetBlogDetailByBid(id)
	blog.BlogContent = bc.BlogContent

	Response.Success(c, 200, 200, blog)
}

func DeleteBlogById(c *gin.Context) {
	id, _ := strconv.Atoi(c.Query("id"))
	if id < 0 {
		Response.Err(c, 200, 400, "id 错误", "")
		return
	}

	var err error
	// 从数据库中删除
	err = new(dao.BlogDao).DeleteBlogById(id)
	if err != nil {
		Response.Err(c, 200, 400, "删除失败, "+err.Error(), "")
		return
	}
	// 删除成功
	Response.Success(c, 200, "删除成功", map[string]interface{}{
		"count": 1,
	})
	return

}

func GetEditPage(c *gin.Context) {
	c.HTML(200, "template/edit.html", gin.H{
		"title": "博客编辑页面",
	})
}

func PostImage(c *gin.Context) {
	file, _ := c.FormFile("file")
	dst := fmt.Sprintf("./static/images/%s", file.Filename)

	err := c.SaveUploadedFile(file, dst)
	if err != nil {
		Response.Err(c, 403, 403, "上传失败", "")
	} else {
		Response.Success(c, 200, "上传成功", map[string]interface{}{
			"url": fmt.Sprintf("http://%s:%d/static/images/%s", global.Settings.Host, global.Settings.Port, file.Filename),
		})
	}
}

func NewBlog(c *gin.Context) {
	blogForm := forms.BlogForm{}
	c.Bind(&blogForm)

	createTime := time.Now()
	status := map[string]int{"草稿": 0, "正文": 1}

	blog := models.Blog{
		BlogTitle:        blogForm.Title,
		BlogContent:      blogForm.Content,
		BlogCategoryName: blogForm.Category,
		BlogTags:         blogForm.Tags,
		BlogStatusStr:    blogForm.Status,
		BlogCoverImage:   blogForm.Img,
		BlogSummary:      blogForm.Summary,
		BlogLikes:        0,
		BlogStatus:       status[blogForm.Status],
		BlogViews:        0,
		CreateTime:       &createTime,
		AuthorID:         1,
	}

	err := new(dao.BlogDao).NewBlog(&blog)
	if err != nil {
		log.Println(err)
	}
	blog.BlogSubUrl = fmt.Sprintf("http://%s:%d/blog/%d/detail", global.Settings.Host, 10001, blog.BlogId)
	blog = new(dao.BlogDao).UpdateUrlById(blog)
	fmt.Println(blog)
	if blog.BlogId > 0 {
		Response.Success(c, 200, "success", map[string]interface{}{
			"blog": blog,
		})
	}

}
